<template>
  <!-- 我是用户表 -->
  <el-card class="userList">
    <!-- 查询部分 -->
    <div class="searchForm">
      <el-form :inline="true" :model="searchForm" size="mini">
        <el-form-item label="名称">
          <el-input v-model="searchForm.p" placeholder="请输入" @keyup.enter.native="m_handleFilter"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="searchForm.type" placeholder="请选择">
            <el-option
              v-for="(item,idex) in rolesOptins"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="m_handleFilter">查询</el-button>
          <el-button type="primary" @click="m_handleClear">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="hh-btnbox">
      <el-button size="mini" @click="handleAddUser">新增</el-button>
    </div>
    <!-- 查询部分！！！ -->

    <!-- 列表 -->
    <div class="table-box">
      <el-table
        ref="multipleTable"
        :data="tableData.list"
        highlight-current-row
        style="width: 100%"
        border
        size="mini"
        stripe
        @selection-change="m_handleSelectionChange"
      >
        <el-table-column align="center" width="55" type="selection" fixed="left"></el-table-column>
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column
          v-for="item in tableData.headList"
          :key="item.name"
          :label="item.title"
          :prop="item.name"
          :show-overflow-tooltip="true"
          align="center"
        >
          <template slot-scope="scope">
            <div v-if="item.name=='images'"></div>
            <div v-else-if="item.name=='type'" :style="filter_userType_color(scope.row.type)">{{filter_userType_label(scope.row.type)}}</div>
            <div v-else>{{scope.row[item.name]}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEditUser(scope.row)">编辑</el-button>
            <el-button size="mini" @click="handleDeUser(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="tableData.total>0"
        :total="tableData.total"
        :page.sync="searchForm.start"
        :limit.sync="searchForm.count"
        @pagination="m_getTableData"
      />
    </div>
    <!-- 列表 -->
    <el-dialog
      :title="dialog.title"
      :visible.sync="dialog.dialogVisible"
      :width="dialog.width"
      :before-close="m_handleClose"
    >
      <div class="main">
        <ae-form :AEform="AEform" @close="close_dialog" @submite="submite_dialog"></ae-form>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  axiosRegister,
  axiosUpRegister,
  axiosDeRegister,
  axiosGetUserList
} from "@/api/user.js";
import { filter_userType, userType } from "@/utils/filter/user";
import pagination from "@/components/Pagination";
import tablePage from "@/mixins/tablePage";
import aeForm from "./aeForm";
export default {
  name: "userList",
  mixins: [tablePage],
  components: {
    pagination,
    aeForm
  },
  data() {
    return {
      dialog: {
        title: "标题",
        dialogVisible: false,
        width: "30%"
      },
      searchForm: {
        //搜索数据
        start: 0,
        count: 10
      },
      fn: axiosGetUserList,
      rolesOptins: userType,
      AEform: {},
      tableData: {
        //表格数据
        headList: [
          { name: "username", title: "用户名" },
          { name: "type", title: "角色" }
        ],
        list: [],
        total: 0
      },
      rows: {} //表行
    };
  },
  computed: {},
  created() {
    this.m_getTableData();
  },
  methods: {
    filter_userType_label(val) {
      return filter_userType(val).label;
    },
    filter_userType_color(val) {
      return filter_userType(val).color;
    },
    close_dialog() {
      this.dialog.dialogVisible = false;
      this.AEform = {};
    },
    submite_dialog(val) {
      if (this.dialog.title === "编辑") {
        axiosUpRegister(val).then(res => {
          this.m_getTableData();
        });
      } else {
        axiosRegister(val).then(res => {
          this.m_getTableData();
        });
      }
      this.dialog.dialogVisible = false;
      this.AEform = {};
    },
    handleAddUser() {
      this.dialog.dialogVisible = true;
      this.dialog.title = "新增";
    },
    handleEditUser(row) {
      this.dialog.dialogVisible = true;
      this.dialog.title = "修改";
      this.AEform = row;
    },
    handleDeUser(row) {
      // 删除
      axiosDeRegister({
        id: row.id
      }).then(res => {
        this.m_handleFilter();
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.userList {
}
</style>
